﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Mime
{
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;

    /// <summary>Provides the base for all field body processors.</summary>
    internal abstract class HeaderFieldBodyProcessor
    {
        /// <summary>Initializes a new instance of the <see cref="HeaderFieldBodyProcessor"/> class.</summary>
        internal HeaderFieldBodyProcessor()
        {
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal abstract void ProcessNormalBodyEvent(byte ev);

        internal abstract void ProcessQuotedBodyEvent(byte ev, bool isQuoteChar);

        internal abstract void ProcessCommentBodyEvent(byte ev);

        [SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "There's no meaningful way how complexity could be reduced.")]
        internal static HeaderFieldBodyProcessor GetProcessor(string fieldName)
        {
            string uppercaseFieldName = fieldName.ToUpper(CultureInfo.InvariantCulture);

            switch (uppercaseFieldName)
            {
                case UppercaseHeaderFieldNames.ResentDate:
                case UppercaseHeaderFieldNames.Date:
                    return new DateHeaderFieldBodyProcessor(fieldName);
                case UppercaseHeaderFieldNames.ResentFrom:
                case UppercaseHeaderFieldNames.ResentSender:
                case UppercaseHeaderFieldNames.ResentTo:
                case UppercaseHeaderFieldNames.ResentCc:
                case UppercaseHeaderFieldNames.ResentBcc:
                case UppercaseHeaderFieldNames.From:
                case UppercaseHeaderFieldNames.Sender:
                case UppercaseHeaderFieldNames.ReplyTo:
                case UppercaseHeaderFieldNames.To:
                case UppercaseHeaderFieldNames.Cc:
                case UppercaseHeaderFieldNames.Bcc:
                    return new AddressListHeaderFieldBodyProcessor(fieldName);
                case UppercaseHeaderFieldNames.Subject:
                case UppercaseHeaderFieldNames.Comments:
                case UppercaseHeaderFieldNames.Keywords:
                    return new DecodingUnstructuredHeaderFieldBodyProcessor(fieldName);
                case UppercaseHeaderFieldNames.InReplyTo:
                case UppercaseHeaderFieldNames.References:
                    return new ReferencesHeaderFieldBodyProcessor(fieldName);
                case UppercaseHeaderFieldNames.ContentType:
                case UppercaseHeaderFieldNames.ContentTransferEncoding:
                case UppercaseHeaderFieldNames.ContentDisposition:
                    return new ParameterizedHeaderFieldBodyProcessor(fieldName);
                default:
                    // See http://www.ietf.org/rfc/rfc2047.txt 5.
                    return uppercaseFieldName.StartsWith("X-", System.StringComparison.Ordinal) ?
                        (HeaderFieldBodyProcessor)new DecodingUnstructuredHeaderFieldBodyProcessor(fieldName) :
                        new UnstructuredHeaderFieldBodyProcessor(fieldName);
            }
        }

        internal abstract HeaderField GetHeaderField();
    }
}
